package org.pz.controller;

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/header")
public class HostController {

    /**
     * 随便造个接口
     */
    @RequestMapping("/i1")
    public String test(HttpServletRequest request) {
        System.out.println( request.getHeader(HttpHeaders.HOST) );
        System.out.println( request.getHeader("aaa") );
        return "1111";
    }

    /**
     * 通过 httpClient 调用 接口
     */
    @RequestMapping("/i2")
    public String test2(HttpServletRequest request) throws IOException {

        System.out.println( System.getProperty("sun.net.http.allowRestrictedHeaders") );

        CloseableHttpClient client = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost("http://localhost:10004/product/test/i1");

        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPost.setHeader(HttpHeaders.HOST, "baidu.com");
        httpPost.setHeader("aaa", "localhost:9005");

        CloseableHttpResponse execute = client.execute(httpPost);
        return execute.getEntity().toString();
    }

}
